/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.CommentDTO;

/**
 *
 * @author User
 */

public class CommentServicesImpl  implements IEntity{
	
	Statement stmt = null;
    ResultSet rst = null;
    private CommentDTO comment;
    private List<CommentDTO> commentList;
    
    
    public CommentDTO GetById(int id) {
        try {
        	
            comment = new CommentDTO();
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from comment where idComment =" + id); //select result set
            comment.setIdComment(id);
            comment.setDescription(rst.getString("description"));
            comment.setDate(rst.getString("date"));
            comment.setIdUser(rst.getInt("idUser"));
            Configuration.CloseConnection();
            		
        } catch (SQLException ex) {
        	
            Logger.getLogger(CommentServicesImpl.class.getName()).log(Level.SEVERE, null, ex);
            
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
        }
        
        return comment;
    }

    public List<CommentDTO> GetAll() {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from comment "); //select result set
            while (rst.next()) {
                comment.setIdComment(rst.getInt("idComment"));
                comment.setDescription(rst.getString("description"));
                comment.setDate(rst.getString("date"));
                comment.setIdUser(rst.getInt("idUser"));
                commentList.add(comment);
            }
            Configuration.CloseConnection();             

        }  
        //INSERT INTO dbsurvey.comment (`idComment`, description, `date`, `idUser`) VALUES (1, 'zxcxzc', '2011-10-19 12:47:16.968', 1)
        catch (SQLException ex) {
            Logger.getLogger(CommentServicesImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
        }
        return commentList;
    }

    public void Add(Object obj) {
       
    CommentDTO objCast=(CommentDTO)obj;
         
       try {			
        	 stmt = Configuration.GetStatement();
        	 stmt.execute("INSERT INTO dbsurvey.comment (`idComment`, description, `date`, `idUser`) VALUES ("+objCast.getIdComment()+", '"+objCast.getDescription()+"', '"+objCast.getDate()+"',"+ objCast.getIdUser()+")");
        	 Configuration.CloseConnection();
         
         } catch (Exception e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
         }
    }

    public void Update(Object obj)  {
    	
    	CommentDTO objCast=(CommentDTO)obj;
        try {
        	
			stmt = Configuration.GetStatement();
			stmt.execute("update dbsurvey.`comment` set  description = "+ objCast.getDescription() + 
        											     "and date = " + objCast.getDate()+
        											     " and idUser = "+ objCast.getIdUser()+
        											     "where idComment = "+ objCast.getIdComment());
			Configuration.CloseConnection();
       	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id)  {
        try {
        	
			stmt = Configuration.GetStatement();
			stmt.execute("delete from `comment`where idComment ="+id);
			Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
